package com.ct.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ct.entity.po.AdvanceApproval;
import com.ct.entity.query.AdvanceApprovalQuery;
import com.ct.entity.vo.ResponseVO;
import com.ct.service.AdvanceApprovalService;

import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;

/**
 * 财务人员审批预支款记录 Controller
 */
@RestController("advanceApprovalController")
@RequestMapping("/advanceApproval")
public class AdvanceApprovalController extends ABaseController {

    // @Autowired
    // private KafkaTemplate<String, Object> kafkaTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Resource
    private AdvanceApprovalService advanceApprovalService;

    /**
     * 根据条件分页查询
     */
    @RequestMapping("/loadDataList")
    public ResponseVO loadDataList(AdvanceApprovalQuery query) {
        return getSuccessResponseVO(advanceApprovalService.findListByPage(query));
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    public ResponseVO add(AdvanceApproval bean) {
        advanceApprovalService.add(bean);
        return getSuccessResponseVO(null);
    }

    /**
     * 批量新增
     */
    @RequestMapping("/addBatch")
    public ResponseVO addBatch(@RequestBody List<AdvanceApproval> listBean) {
        advanceApprovalService.addBatch(listBean);
        return getSuccessResponseVO(null);
    }

    /**
     * 批量新增/修改
     */
    @RequestMapping("/addOrUpdateBatch")
    public ResponseVO addOrUpdateBatch(@RequestBody List<AdvanceApproval> listBean) {
        advanceApprovalService.addBatch(listBean);
        return getSuccessResponseVO(null);
    }

    /**
     * 根据AdvanceApprovalId查询对象
     */
    @RequestMapping("/getAdvanceApprovalByAdvanceApprovalId")
    public ResponseVO getAdvanceApprovalByAdvanceApprovalId(Integer advanceApprovalId) {
        return getSuccessResponseVO(advanceApprovalService.getAdvanceApprovalByAdvanceApprovalId(advanceApprovalId));
    }

    /**
     * 根据AdvanceApprovalId修改对象
     */
    @RequestMapping("/updateAdvanceApprovalByAdvanceApprovalId")
    public ResponseVO updateAdvanceApprovalByAdvanceApprovalId(AdvanceApproval bean, Integer advanceApprovalId) {
        advanceApprovalService.updateAdvanceApprovalByAdvanceApprovalId(bean, advanceApprovalId);
        return getSuccessResponseVO(null);
    }

    /**
     * 根据AdvanceApprovalId删除
     */
    @RequestMapping("/deleteAdvanceApprovalByAdvanceApprovalId")
    public ResponseVO deleteAdvanceApprovalByAdvanceApprovalId(Integer advanceApprovalId) {
        advanceApprovalService.deleteAdvanceApprovalByAdvanceApprovalId(advanceApprovalId);
        return getSuccessResponseVO(null);
    }

    /**
     * 分页查询预支款审批记录
     * 
     * @param userId   工号
     * @param pageNo   页码
     * @param pageSize 页大小
     * @return 分页结果
     */
    @RequestMapping("/getApproveAdvanceRecord")
    public ResponseVO getApproveAdvanceRecord(@NotEmpty String userId, Integer pageNo, Integer pageSize) {
        // kafkaTemplate.send("advance-approval-topic", userId);
        System.out.println("AdvanceApprovalController.getApproveAdvanceRecord()");
        String queueName = "simple.queue";
        String message = "hello, spring amqp!";
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("message", message);
        rabbitTemplate.convertAndSend(queueName, map);
        return getSuccessResponseVO(advanceApprovalService.getApproveAdvanceRecord(userId, pageNo, pageSize));
    }

    @RequestMapping("/getAdvanceApprovalByAdvanceId")
    public ResponseVO getAdvanceApprovalByAdvanceId(@RequestParam Integer advanceId) {
        return getSuccessResponseVO(advanceApprovalService.getAdvanceApprovalByAdvanceId(advanceId));
    }
}